//
//  WBNetworkManager.swift
//  Weibo-Swift
//
//  Created by law on 2018/8/30.
//  Copyright © 2018年 JKing. All rights reserved.
//

import UIKit
import Alamofire
import SVProgressHUD

class WBNetworkManager: NSObject {
    
    /// validate token
    var accessToken: String? = "2.00n9eEMDTGRnvC06c030e668IcYkrB===kkk"
    
    /// singleton
    static let shared = WBNetworkManager()
    
    /// 带token验证的请求
    ///
    /// - Parameters:
    ///   - url: The URL.
    ///   - method: The HTTP method. `.get` by default.
    ///   - parameters: The parameters. `nil` by default.
    ///   - completion: A closure to be executed once the request has finished.
    func tokenRequest(
        _ url: String,
        method: HTTPMethod = .get,
        parameters: [String: Any]?,
        completion: @escaping (DataResponse<Any>)->Void)
        -> Void
    {
        // 验证token
        if accessToken == nil {
            SVProgressHUD.showInfo(withStatus: "token过期了")
        }
        
        var parameters = parameters
        if parameters == nil {
            parameters = [String: AnyObject]()
        }
        parameters!["accessToken"] = accessToken
        
        Alamofire.request(url,
                          method: method,
                          parameters: parameters).responseJSON { (response) in
            
            // token 过期
            if response.response?.statusCode == 403 {
                SVProgressHUD.showInfo(withStatus: "token过期了")
            }
            completion(response)
            
        }
    }
    
}
